﻿var map = L.map("l-map", { attributionControl: false }).setView(mapcenter, 5);
map.zoomControl.setPosition("bottomright");
L.tileLayer("http://t4.tianditu.com/DataServer?T=vec_w&x={x}&l={z}&y={y}", { maxZoom: 16, attribution: "" }).addTo(map);
L.tileLayer("http://t0.tianditu.com/DataServer?T=cva_w&x={x}&l={z}&y={y}", { maxZoom: 16, attribution: "" }).addTo(map);
var b = [new L.LatLng(34, 127), new L.LatLng(22, 127), new L.LatLng(18, 120), new L.LatLng(11, 120), new L.LatLng(4.5, 113), new L.LatLng(0, 105)], c = [new L.LatLng(34, 132), new L.LatLng(15, 132), new L.LatLng(0, 120), new L.LatLng(0, 105)];
L.polyline(b, { color: "#ffff00", weight: 1.5 }).addTo(map);
L.polyline(c, { color: "#0000ff", weight: 0.8, dashArray: "10,8" }).addTo(map);
function cnWindir(a) { } 
var markers = [];
function markerDelAgain() {
    for (var a = 0; a < markers.length; a++) map.removeLayer(markers[a]);
    markers = [];
}
var myIcon = L.icon({ iconUrl: "Content/images/typhoon/t-00.png", iconSize: [12, 12] }), points = [];
function getIcon(a) {
    var f = L.icon({ iconUrl: "Content/images/typhoon/t-00.png", iconSize: [12, 12] });
    switch (a) {
        case 6:
        case 7:
            f = L.icon({ iconUrl: "Content/images/typhoon/t-01.png", iconSize: [12, 12] });
            break;
        case 8:
        case 9:
            f = L.icon({ iconUrl: "Content/images/typhoon/t-02.png", iconSize: [12, 12] });
            break;
        case 10:
        case 11:
            f = L.icon({ iconUrl: "Content/images/typhoon/t-03.png", iconSize: [12, 12] });
            break;
        case 12:
        case 13:
            f = L.icon({ iconUrl: "Content/images/typhoon/t-04.png", iconSize: [12, 12] });
            break;
        case 14:
        case 15:
            f = L.icon({iconUrl: "Content/images/typhoon/t-05.png",iconSize: [12, 12]});
            break;
        case 16:
        case 17:
        case 18:
            f = L.icon({ iconUrl: "Content/images/typhoon/t-06.png", iconSize: [12, 12] });
            break;
    }
    return f;
}

function getPopupContent(a, f, d) {
    var e = "<table>";
    if (f) {
        e += "<tr><th>预报机构：</th><td>" + d + "</td></tr>";
        e += "<th>到达时间：</th><td>" + a.StrTime + "</td></tr>";
    } else e += "<th>过去时间：</th><td>" + a.StrTime + "</td></tr>";
    e += a.Strong != "" && a.Strong != null ? "<tr><th>强&#12288;&#12288;度：</th><td>" + a.Strong + "</td></tr>" : "";
    e += "<tr><th>中心位置：</th><td>" +a.Longitude + "°E，" + a.Latitude + "°N</td></tr>";
    e += a.Power != null || a.Speed != null ? "<tr><th>风力风速：</th><td>" + a.Power + "级，" + a.Speed + "米/秒</td></tr>" : "";
    e += a.Pressure != null ? "<tr><th>中心气压：</th><td>" + a.Pressure + "百帕</td></tr>" : "";
    e += a.MovingSpeed != "" || a.Moves != "" ? "<tr><th>移速移向：</th><td>" + a.MovingSpeed + "公里/小时，" +a.Moves + "</td></tr>" : "";
    if (a.Radius7 != null || a.Radius10 != null || a.Radius12 != null) {
        e += "<th style=\"vertical-align:text-top;\">风圈半径：</th><td>";
        e += a.Radius7 != null ? a.Radius7 + "公里（七级）" : "";
        e += a.Radius10 != null ? "<br/>" + a.Radius10 + "公里（十级）" : "";
        e += a.Radius12 != null ? "<br/>" + a.Radius12 + "公里（十二级）" : "";
        e += "</td></tr>";
    }
    e += "</table>";
    return e;
}

function loadData(a) {
    $.getJSON("GetTyphoon?id=" + a + "&_d=" + new Date, function (f) {
        $("#typhoonName").html(f.Code + f.Name + "(" + f.EName + ")");
        points = f.Points;
        $.each(f.Points, function (e, g) {
            myIcon = getIcon(g.Power);
            e = getPopupContent(g, false, "");
            g = L.marker([g.Latitude, g.Longitude], { icon: myIcon });
            g.bindPopup(e);
            markers.push(g);
        });
        var d = 0;
        lj = window.setInterval(function () {
            if (d < markers.length) {
                if (d == markers.length - 1) {
                    if (points[d].Radius7 != null)
                        var e = L.circle([points[d].Latitude, points[d].Longitude], points[d].Radius7 *
                            1E3, { color: "#00B00F", fillColor: "#00B00F", weight: 1, fillOpacity: 0.3 }).addTo(map);
                    points[d].Radius10 != null && L.circle([points[d].Latitude, points[d].Longitude], points[d].Radius10 * 1E3, { color: "#FF0000", fillColor: "#FF0000", weight: 1, fillOpacity: 0.3 }).addTo(map);
                    L.marker([markers[d].getLatLng().lat, markers[d].getLatLng().lng], { icon: myIcon = L.icon({ iconUrl: "Content/images/typhoon/typhoon.gif", iconSize: [40, 40] }) }).addTo(map);
                }
                d > 0 && (new L.Polyline([[markers[d - 1].getLatLng().lat, markers[d - 1].getLatLng().lng], [markers[d].getLatLng().lat,
                    markers[d].getLatLng().lng]], { weight: 2, color: "#000000" })).addTo(map);
                markers[d].addTo(map);
                if (d == markers.length - 1) {
                    map.setView([points[d].Latitude, points[d].Longitude], 5);
                    forecast(d);
                    markers[d].openPopup();
                }
                d++;
            } else window.clearInterval(lj);
        }, 80);
    });
}
function forecast(a) {
    points[a].Forecasts != null && $.each(points[a].Forecasts, function (f, d) {
        var e = "#000000";
        switch (d.Sets) {
            case "中国":
                e = "#FF4050";
                break;
            case "中国香港":
                e = "#FF66FF";
                break;
            case "日本":
                e = "#43FF4B";
                break;
            case "韩国":
                e = "#669999";
                break;
            case "美国":
                e = "#40DDFF";
                break;
        }
        $.each(d.Points, function (g, h) {
            if (g > 0) {
                (new L.Polyline([[d.Points[g - 1].Latitude, d.Points[g - 1].Longitude], [h.Latitude, h.Longitude]], {
                    weight: 2,
                    color: e,
                    dashArray: "10,8"
                })).addTo(map);
                myIcon = getIcon(h.Power);
                g = getPopupContent(h, true, d.Sets);
                h = L.marker([h.Latitude, h.Longitude], { icon: myIcon });
                h.bindPopup(g).addTo(map);
            }
        });
    });
} var lj;


var s = { "Code": "1416", "Name": "凤凰", "EName": "FUNG-WONG", "StartDate": "2014-09-18T02:00:00", "EndDate": "2014-09-21T23:00:00",
    "Points": [
        { "StrTime": "2014年09月18日02时", "Longitude": 129.6, "Latitude": 13.2, "Strong": "热带风暴", "Power": 8, "Speed": 18, "Moves": "西北", "MovingSpeed": "30", "Pressure": 998, "Radius7": 200, "Radius10": null, "Radius12": null, "Forecasts": null },
        { "StrTime": "2014年09月18日05时", "Longitude": 128.9, "Latitude": 13.2, "Strong": "热带风暴", "Power": 8, "Speed": 18, "Moves": "西北", "MovingSpeed": "27", "Pressure": 998, "Radius7": 200, "Radius10": null, "Radius12": null, "Forecasts": null },
        { "StrTime": "2014年09月18日08时", "Longitude": 128.3, "Latitude": 13.4, "Strong": "热带风暴", "Power": 8, "Speed": 18, "Moves": "西北", "MovingSpeed": "30", "Pressure": 998, "Radius7": 200, "Radius10": null, "Radius12": null, "Forecasts": null },
        { "StrTime": "2014年09月18日11时", "Longitude": 127.9, "Latitude": 13.8, "Strong": "热带风暴", "Power": 8, "Speed": 18, "Moves": "西北", "MovingSpeed": "30", "Pressure": 998, "Radius7": 200, "Radius10": null, "Radius12": null, "Forecasts": null },
        { "StrTime": "2014年09月21日08时", "Longitude": 120.2, "Latitude": 21.8, "Strong": "强热带风暴", "Power": 10, "Speed": 28, "Moves": "北", "MovingSpeed": "25", "Pressure": 982, "Radius7": 180, "Radius10": 50, "Radius12": null, "Forecasts": null },
        { "StrTime": "2014年09月21日09时", "Longitude": 120.4, "Latitude": 21.8, "Strong": "强热带风暴", "Power": 10, "Speed": 28, "Moves": "北", "MovingSpeed": "25", "Pressure": 982, "Radius7": 180, "Radius10": 50, "Radius12": null, "Forecasts":
            [
                { "Sets": "日本", "Points":
                    [
                        { "StrTime": "2014年09月21日08时", "Longitude": 120.2, "Latitude": 21.8, "Strong": "强热带风暴", "Power": 10, "Speed": 28, "Moves": "北", "MovingSpeed": "25", "Pressure": 982, "Radius7": 180, "Radius10": 50, "Radius12": null, "Forecasts": null },
                        { "StrTime": "2014年09月22日08时", "Longitude": 121.5, "Latitude": 26.3, "Strong": "热带风暴", "Power": 9, "Speed": 23, "Moves": "北", "MovingSpeed": "12KT", "Pressure": 985, "Radius7": null, "Radius10": null, "Radius12": null, "Forecasts": null }
                    ]
                },
                { "Sets": "韩国", "Points":
                    [
                        { "StrTime": "2014年09月21日08时", "Longitude": 120.2, "Latitude": 21.8, "Strong": "强热带风暴", "Power": 10, "Speed": 28, "Moves": "北", "MovingSpeed": "25", "Pressure": 982, "Radius7": 180, "Radius10": 50, "Radius12": null, "Forecasts": null },
                        { "StrTime": "2014年09月22日08时", "Longitude": 120.9, "Latitude": 26.7, "Strong": "热带风暴", "Power": 9, "Speed": 24, "Moves": "", "MovingSpeed": "", "Pressure": 990, "Radius7": null, "Radius10": null, "Radius12": null, "Forecasts": null },
                        { "StrTime": "2014年09月25日08时", "Longitude": 131.8, "Latitude": 36.1, "Strong": null, "Power": null, "Speed": null, "Moves": "", "MovingSpeed": "", "Pressure": null, "Radius7": null, "Radius10": null, "Radius12": null, "Forecasts": null }
                    ]
                }
            ]
        }]
};